home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jed096_1.zip / SLANG / SRC / SLSTD.C < prev    next >
C/C++ Source or Header  |  1994-04-26  |  36KB  |  1,317 lines

  1. /* Basic string functions for S-Lang */
  2. /* 
  3.  * Copyright (c) 1992, 1994 John E. Davis 
  4.  * All rights reserved.
  5.  *
  6.  * Permission is hereby granted, without written agreement and without
  7.  * license or royalty fees, to use, copy, and distribute this
  8.  * software and its documentation for any purpose, provided that the
  9.  * above copyright notice and the following two paragraphs appear in
  10.  * all copies of this software.
  11.  *
  12.  * IN NO EVENT SHALL JOHN E. DAVIS BE LIABLE TO ANY PARTY FOR DIRECT,
  13.  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
  14.  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JOHN E. DAVIS
  15.  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * JOHN E. DAVIS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19.  * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  20.  * BASIS, AND JOHN E. DAVIS HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
  21.  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  22.  */
  23.  
  24.  
  25. #include <stdio.h>
  26. #ifdef __cplusplus
  27. #include <stdlib.h>
  28. #else
  29. extern int system();
  30. #endif
  31.  
  32. #include "slang.h"
  33. #include "_slang.h"
  34. #include "slarray.h"
  35.  
  36.  
  37. /* Standard intrinsic functions for S-Lang.  Included here are string
  38.    and array operations */
  39.       
  40. /* builtin stack manipulation functions */
  41.  
  42. void SLdo_pop(void)
  43. {
  44.    SLang_Object_Type x;
  45.    if (SLang_pop(&x)) return;
  46.  
  47.    if (IS_DATA_STRING(x)) FREE(x.v.s_val);
  48. }
  49.  
  50. int SLdo_dup(void)
  51. {
  52.    SLang_Object_Type x;
  53.    if (SLang_pop(&x)) return(0);
  54.    SLang_push(&x);
  55.    if ((x.type >> 8) == STRING_TYPE) SLang_push_string(x.v.s_val);
  56.    else SLang_push (&x);
  57.    
  58.    return(1);
  59. }
  60.    
  61.  
  62. void SLdo_strcat(void)
  63. {
  64.    char *a, *b, *c;
  65.    int len, lena;
  66.    int adata, bdata;
  67.  
  68.    if (SLang_pop_string(&b, &bdata) || SLang_pop_string(&a, &adata)) return;
  69.  
  70.    lena = strlen(a);
  71.    len = lena + strlen(b) + 1;
  72.    if (adata == 1)
  73.      {
  74.     if ((NULL != (c = (char *) REALLOC(a, len))))
  75.       {
  76.          strcpy (c + lena, b);
  77.          adata = 0;
  78.       }
  79.     else
  80.       {
  81.          SLang_Error = SL_MALLOC_ERROR;
  82.          return;
  83.       }
  84.      }
  85.    else if (NULL != (c = (char *) MALLOC(len)))
  86.      {
  87.     strcpy(c, a);
  88.     strcpy(c + lena, b);
  89.      }
  90.    else 
  91.      {
  92.     SLang_Error = SL_MALLOC_ERROR; /* SLang_doerror("Lang Malloc error."); */
  93.     return;
  94.      }
  95.    
  96.    /* instead of going throug push string, push it directly */
  97.    SLang_push_malloced_string(c);
  98.    if (adata == 1) FREE(a);
  99.    if (bdata == 1) FREE(b);
  100. }
  101.  
  102. void SLdo_strtrim(void)
  103. {
  104.    char *a, *beg, *end, *c, *neew, ch;
  105.    int len;
  106.    int adata;
  107.  
  108.    if (SLang_pop_string(&a, &adata)) return;
  109.  
  110.    len = strlen(a);
  111.    beg = a;
  112.    end = a + (len - 1);
  113.    while (ch = *beg, (ch == ' ') || (ch == '\t') || (ch == '\n')) beg++;
  114.    while (end >= beg)
  115.      {
  116.     ch = *end;
  117.     if ((ch == ' ') || (ch == '\t') || (ch == '\n')) end--;
  118.     else break;
  119.      }
  120.    end++;
  121.    len = (int) (end - beg);
  122.    /* instead of going throug push string, push it directly.  This is because
  123.     * the string that is being copied might be a constant. */
  124.    if (NULL != (c = (char *) MALLOC(len + 1)))
  125.      {
  126.     neew = c;
  127.     while (beg < end) *c++ = *beg++;
  128.     *c = 0;
  129.  
  130.     SLang_push_malloced_string(neew);
  131.      }
  132.    else SLang_Error = SL_MALLOC_ERROR; /* SLang_doerror("Lang Malloc error."); */
  133.    
  134.    if (adata == 1) FREE(a);
  135. }
  136.  
  137. static char Utility_Char_Table [256];
  138.  
  139. static void set_utility_char_table (char *pos)
  140. {
  141.    register char *t = Utility_Char_Table, *tmax;
  142.    register unsigned char ch;
  143.  
  144.    tmax = t + 256;
  145.    while (t < tmax) *t++ = 0;
  146.    
  147.    t = Utility_Char_Table;
  148.    while ((ch = (unsigned char) *pos++) != 0) t[ch] = 1;
  149. }
  150.  
  151. /* This routine returns the string with text removed between single character
  152.    comment delimeters from the set b and e. */
  153.  
  154. static void uncomment_string (char *str, char *b, char *e)
  155. {
  156.    unsigned char chb, che;
  157.    unsigned char *s, *cbeg, *mark;
  158.    
  159.    if (strlen(b) != strlen(e))
  160.      {
  161.     SLang_doerror ("Comment delimeter length mismatch.");
  162.     return;
  163.      }
  164.    
  165.    set_utility_char_table (b);
  166.    
  167.    if (NULL == (str = (char *) SLmake_string(str))) return;
  168.    
  169.    s = (unsigned char *) str;
  170.    
  171.    while ((chb = *s++) != 0)
  172.      {
  173.     if (Utility_Char_Table [chb] == 0) continue;
  174.     
  175.     mark = s - 1;
  176.  
  177.     cbeg = (unsigned char *) b;
  178.     while (*cbeg != chb) cbeg++;
  179.     
  180.     che = (unsigned char) *(e + (int) (cbeg - (unsigned char *) b));
  181.     
  182.     while (((chb = *s++) != 0) && (chb != che));
  183.       
  184.     if (chb == 0)
  185.       {
  186.          /* end of string and end not found.  Just truncate it a return; */
  187.          *mark = 0;
  188.          break;
  189.       }
  190.     
  191.     strcpy ((char *) mark, (char *)s);
  192.     s = mark;
  193.      }
  194.    SLang_push_malloced_string (str);
  195. }
  196.  
  197.    
  198.    
  199. void SLquote_string ()
  200. {
  201.    char *str, *quotes, *q;
  202.    int sdata, qdata;
  203.    int slash, n;
  204.    register char *t, *s, *q1;
  205.    register unsigned char ch;
  206.    
  207.    if ((SLang_pop_integer (&slash))    /* quote char */
  208.        || (SLang_pop_string ("es, &qdata))) return;   /* chars to quote */
  209.    if (SLang_pop_string (&str, &sdata))  /* string to quote */
  210.      {
  211.     if (qdata == 1) FREE (quotes);
  212.     return;
  213.      }
  214.    
  215.    /* setup the utility table to have 1s at quote char postitions. */
  216.    set_utility_char_table (quotes);
  217.    
  218.    t = Utility_Char_Table;
  219.    t[(unsigned int) slash] = 1;
  220.    
  221.    /* calculate length */
  222.    s = str;
  223.    n = 0;
  224.    while ((ch = (unsigned char) *s++) != 0) if (t[ch]) n++;
  225.    n += (int) (s - str);
  226.    
  227.    if (NULL != (q = (char *) MALLOC(n)))
  228.      {
  229.     s = str; q1 = q;
  230.     while ((ch = (unsigned char) *s++) != 0)
  231.       {
  232.          if (t[ch]) *q1++ = slash;
  233.          *q1++ = (char) ch;
  234.       }
  235.     *q1 = 0;
  236.     SLang_push_malloced_string(q);
  237.      }
  238.    else SLang_Error = SL_MALLOC_ERROR;
  239.    
  240.    if (qdata == 1) FREE (quotes);
  241.    if (sdata == 1) FREE (str);
  242. }
  243.  
  244.  
  245. /* returns the position of substrin in a string or null */
  246. void SLdo_issubstr(void)
  247. {
  248.    char *a, *b, *c;
  249.    int adata, bdata, n;
  250.  
  251.    if (SLang_pop_string(&b, &bdata) || SLang_pop_string(&a, &adata)) return;
  252.  
  253.    if (NULL == (c = (char *) strstr(a, b))) n = 0; else n = 1 + (int) (c - a);
  254.  
  255.    if (adata == 1) FREE(a);
  256.    if (bdata == 1) FREE(b);
  257.    SLang_push_integer (n);
  258. }
  259.  
  260. /* returns to stack string at pos n to n + m of a */
  261. void SLdo_substr(void)
  262. {
  263.    char *a;
  264.    int adata, n, m;
  265.    char b[256];
  266.  
  267.    if (SLang_pop_integer(&m) || SLang_pop_integer(&n) || (SLang_pop_string(&a, &adata))) return;
  268.  
  269.    *b = 0;
  270.    if (m > 0)
  271.      {
  272.     strncpy(b, a + (n - 1), 254);
  273.     if (m > 255) m = 255;
  274.     b[m] = 0;
  275.      }
  276.    if (adata == 1) FREE(a);
  277.    SLang_push_string(b);
  278. }
  279. /* substitute char m at positin string n in string*/
  280. void SLdo_strsub(void)
  281. {
  282.    char *a;
  283.    int adata, n, m;
  284.    char b[256];
  285.  
  286.    if (SLang_pop_integer(&m) || SLang_pop_integer(&n) || (SLang_pop_string(&a, &adata))) return;
  287.  
  288.    strncpy(b, a, 254);
  289.    b[254] = 0;
  290.    if (adata == 1) FREE(a);
  291.    if ((n < 1) || (n > 254)) n = 254;
  292.    b[n-1] = (char) m;
  293.    SLang_push_string(b);
  294. }
  295.  
  296. void SLdo_strup(void)
  297. {
  298.    unsigned char c, *a;
  299.    int adata;
  300.  
  301.    if (SLang_pop_string((char **) &a, &adata)) return;
  302.    SLang_push_string((char *) a);
  303.    if (adata == 1) FREE(a);
  304.    
  305.    a = (unsigned char *) (SLStack_Pointer - 1)->v.s_val;
  306.    while ((c = *a) != 0)
  307.      {
  308.     /* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */
  309.     *a = UPPER_CASE(c);
  310.     a++;
  311.      }
  312. }
  313.  
  314. void SLdo_strlow(void)
  315. {
  316.    unsigned char c, *a;
  317.    int adata;
  318.  
  319.    if (SLang_pop_string((char **) &a, &adata)) return;
  320.    SLang_push_string((char *) a);
  321.    if (adata == 1) FREE(a);
  322.    a = (unsigned char *) (SLStack_Pointer - 1)->v.s_val;
  323.    
  324.    while ((c = *a) != 0)
  325.      {
  326.     /* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */
  327.     *a = LOWER_CASE(c);
  328.     a++;
  329.      }
  330. }
  331.  
  332. void SLdo_strcmp(void)
  333. {
  334.    char *a, *b;
  335.    int adata, bdata, i;
  336.  
  337.    if (SLang_pop_string(&b, &bdata) || SLang_pop_string(&a, &adata)) return;
  338.  
  339.    i = strcmp(a, b);
  340.  
  341.    if (adata == 1) FREE(a);
  342.    if (bdata == 1) FREE(b);
  343.    SLang_push_integer (i);
  344. }
  345.  
  346. void SLdo_strncmp(void)
  347. {
  348.    char *a, *b;
  349.    int adata, bda